home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Test / DumpCON / DumpCON.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  10KB  |  365 lines

  1. /* Incredibly Basic Terminal Program (C) 1995 Hydra/Tension
  2.  
  3.    DumpCON Part of the development for HBBS!
  4.  
  5.    read input from serial port and/or console window and dumps input as decimal
  6.    numbers..
  7.  
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15.  
  16. #include <exec/exec.h>
  17. #include <intuition/intuition.h>
  18. #include <devices/console.h>
  19. #include <devices/serial.h>
  20. #include <dos/dos.h>
  21. #include <libraries/reqtools.h>
  22.  
  23. #include <clib/exec_protos.h>
  24. #include <clib/alib_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/console_protos.h>
  27. #include <clib/dos_protos.h>
  28. #include <clib/reqtools_protos.h>
  29.  
  30. #define ClrSignal(s)  SetSignal(0,s)
  31.  
  32. extern struct GfxBase *GfxBase;
  33. extern struct IntuitionBase *IntuitionBase;
  34. struct ReqToolsBase *ReqToolsBase;
  35. struct Window *Win=NULL;
  36. struct Screen *Scr=NULL;
  37.  
  38. struct IOStdReq *ConRead;
  39. struct IOStdReq *ConWrite;
  40. struct MsgPort *ConRPort;
  41. struct MsgPort *ConWPort;
  42.  
  43. struct IOExtSer *SerRead;
  44. struct IOExtSer *SerWrite;
  45. struct MsgPort *SerPort;
  46. //struct MsgPort *SerReadPort;
  47.  
  48. long __stack = 40000;
  49.  
  50.   char ConBuffer[2048];
  51.   char SerBuffer[2048];
  52.   char SerialInChar;
  53.   ULONG SerBufLen;
  54.  
  55. BOOL SerFlag=FALSE;
  56. BOOL ConFlag=FALSE;
  57.  
  58. void CloseLibs( void )
  59. {
  60. if (NULL != GfxBase )
  61.   CloseLibrary( ( struct Library * )GfxBase );
  62. if (NULL != IntuitionBase )
  63.   CloseLibrary( ( struct Library * )IntuitionBase );
  64.   if (ReqToolsBase)
  65.     CloseLibrary ((struct Library *)ReqToolsBase);
  66.   exit (0);
  67. }
  68.  
  69. int OpenLibs( void )
  70. {
  71.   if (ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))
  72.     if ( NULL != (GfxBase = (struct GfxBase * )OpenLibrary((UBYTE *)"graphics.library" , 37)))
  73.       if ( NULL != (IntuitionBase = (struct IntuitionBase * )OpenLibrary((UBYTE *)"intuition.library" , 37)))
  74.     return( 0L );
  75.   CloseLibs();
  76. return( 1L );
  77. }
  78.  
  79. BYTE OpenConsole(struct IOStdReq *writereq, struct IOStdReq *readreq, struct Window *window)
  80. {
  81.   BYTE error;
  82.  
  83.   // point to window..
  84.  
  85.   writereq->io_Data = (APTR) window;
  86.   writereq->io_Length = sizeof(struct Window);
  87.  
  88.   // open device..
  89.  
  90.   error = OpenDevice("console.device", 0, (struct IORequest*)writereq, 0);
  91.  
  92.   // duplicate data..
  93.  
  94.   readreq->io_Device = writereq->io_Device; /* clone required parts */
  95.   readreq->io_Unit   = writereq->io_Unit;
  96.  
  97.   return(error);
  98. }
  99.  
  100. void cancelcon( void )
  101. {
  102.   if (ConFlag)
  103.   {
  104.     AbortIO((struct IORequest *)ConRead);
  105.     WaitIO((struct IORequest *)ConRead);
  106.     ConFlag=FALSE;
  107.   }
  108. }
  109.  
  110. void readcon( void )
  111. {
  112.   if (!ConFlag)
  113.   {
  114.     ConRead->io_Command  = CMD_READ;
  115.     ConRead->io_Data     = (APTR)ConBuffer;
  116.     ConRead->io_Length   = 1;
  117.     ClrSignal(1L << ConRPort->mp_SigBit);
  118.     SendIO((struct IORequest*)ConRead);
  119.     ConFlag=TRUE;
  120.   }
  121. }
  122.  
  123. void ConWriteData(UBYTE *data,ULONG length)
  124. {
  125.   cancelcon();
  126.   ConWrite->io_Command  = CMD_WRITE;
  127.   ConWrite->io_Data     = data;
  128.   ConWrite->io_Length   = length;
  129.  
  130.   DoIO((struct IORequest *)ConWrite);
  131.   readcon();
  132. }
  133.  
  134. void cancelser( void )
  135. {
  136.   if (SerFlag)
  137.   {
  138.     AbortIO((struct IORequest *)SerRead);
  139.     WaitIO((struct IORequest *)SerRead);
  140.     SerFlag=FALSE;
  141.   }
  142. }
  143.  
  144. void readser( void )
  145. {
  146.  
  147.   if (!SerFlag)
  148.   {
  149.     SerRead->IOSer.io_Command  = CMD_READ;
  150.     SerRead->IOSer.io_Data     = &SerialInChar;
  151.     SerRead->IOSer.io_Length   = 1;
  152.     ClrSignal(1L << SerPort->mp_SigBit);
  153.     SendIO((struct IORequest*)SerRead);
  154.     SerFlag=TRUE;
  155.   }
  156. }
  157.  
  158. void SerWriteData(UBYTE *data,ULONG length)
  159. {
  160.   cancelser();
  161.   SerWrite->IOSer.io_Command  = CMD_WRITE;
  162.   SerWrite->IOSer.io_Data     = data;
  163.   SerWrite->IOSer.io_Length   = length;
  164.  
  165.   DoIO((struct IORequest *)SerWrite);
  166. }
  167.  
  168. void DumpData(UBYTE *data,ULONG length)
  169. {
  170.   int loop;
  171.   for (loop=0; loop < length ; loop++ )
  172.   {
  173.     printf("%d ",data[loop]);
  174.   }
  175.   fflush(stdout);
  176. }
  177.  
  178. void HandleIt( void )
  179. {
  180.   struct IntuiMessage *IMsg;
  181.   BOOL done=FALSE;
  182.   ULONG SerSigs,ConSigs,WinSigs,ReturnedSigs;
  183.  
  184.  
  185.   while (!done)
  186.   {
  187.     if (!SerFlag) readser();
  188.     if (!ConFlag) readcon();
  189.  
  190.     ConSigs=ConFlag ? 1L << ConRPort->mp_SigBit : 0;
  191.     SerSigs=SerFlag ? 1L << SerPort->mp_SigBit : 0;
  192.     WinSigs=1L << Win->UserPort->mp_SigBit;
  193.  
  194.     // the sigbreak bit can ONLY be activated if another program sends a break
  195.     // to this program, something like artm or taske might be usefull! :-)
  196.     // you can't type ctrl-c cos the console.device gets there first!
  197.  
  198.  
  199.     // put the task to sleep until we get some input!
  200.  
  201.     ReturnedSigs=Wait(SerSigs | ConSigs | WinSigs | SIGBREAKF_CTRL_C);
  202.  
  203.     // check to see what input happened
  204.  
  205.     if (ReturnedSigs & SIGBREAKF_CTRL_C) done=TRUE;
  206.  
  207.     // something happened to the window..
  208.     if (ReturnedSigs & WinSigs)
  209.     {
  210.       while (IMsg=(struct IntuiMessage *)GetMsg(Win->UserPort))
  211.       {
  212.         switch (IMsg->Class)
  213.         {
  214.           case IDCMP_CLOSEWINDOW:
  215.             done=rtEZRequest("Quit ?","Yes|No",NULL,NULL,NULL);
  216.             break;
  217.         }
  218.         ReplyMsg((struct Message*)IMsg);
  219.       }
  220.     }
  221.     // if we have submitted a sendIO() to the console device check
  222.     // to see if a) the signal was from the console or b) check to see if the request
  223.     // has been completed,  this is usefull if you get a serial signal AND the
  224.     // computer was just about to send us a console signal..
  225.     if (ConFlag && ((ReturnedSigs & ConSigs) || CheckIO((struct IORequest *)ConRead)))
  226.     {
  227.       WaitIO((struct IORequest *)ConRead);
  228.       ConFlag=FALSE;
  229.       if (ConRead->io_Actual)
  230.       {
  231. //        ConWriteData(ConBuffer,ConRead->io_Actual);
  232.         DumpData(ConBuffer,ConRead->io_Actual);
  233.         SerWriteData(ConBuffer,ConRead->io_Actual);
  234.       }
  235.     }
  236.  
  237.     // same as above except for serial instead of console..
  238.  
  239.     if (SerFlag && ((ReturnedSigs & SerSigs) || CheckIO((struct IORequest *)SerRead)))
  240.     {
  241.       // get the one byte of data we requested..
  242.       WaitIO((struct IORequest *)SerRead);
  243.       SerFlag=FALSE;
  244.       SerBuffer[0]=SerialInChar;
  245.       ConWriteData(SerBuffer,1); // and write it to the console..
  246. //      DumpData(SerBuffer,1);
  247.       // then see if there's any more data to be read
  248.       // if there is write it to the console until there's no more left..
  249.       // nomally you might not want to do it like this as there is no way
  250.       // for the user to abort it!
  251.  
  252.       SerRead->IOSer.io_Command  = SDCMD_QUERY;
  253.       SerRead->IOSer.io_Actual = 0;
  254.       DoIO((struct IORequest*)SerRead);
  255.       do
  256.       {
  257.         // any data ?
  258.         if (SerRead->IOSer.io_Actual)
  259.         {
  260.           // yup! read it then!
  261.           SerRead->IOSer.io_Command  = CMD_READ;
  262.           SerRead->IOSer.io_Data     = &SerBuffer[0]; // be explicit, the [0] is not REALLY needed tho..
  263.                                        // don't forget not to overflow our buffer!
  264.           SerRead->IOSer.io_Length   = SerRead->IOSer.io_Actual>2048 ? 2048 : SerRead->IOSer.io_Actual ;
  265.           DoIO((struct IORequest*)SerRead);
  266.  
  267.           SerBufLen=SerRead->IOSer.io_Actual;
  268.           ConWriteData(SerBuffer,SerBufLen);
  269. //          DumpData(SerBuffer,SerBufLen);
  270.         }
  271.         // send another query!
  272.         SerRead->IOSer.io_Command  = SDCMD_QUERY;
  273.         SerRead->IOSer.io_Actual = 0;
  274.         DoIO((struct IORequest*)SerRead);
  275.       }
  276.       while (SerRead->IOSer.io_Actual); // keep checking until no data exists..
  277.     }
  278.   }
  279.   // quit!
  280.   if (SerFlag) cancelser();
  281.   if (ConFlag) cancelcon();
  282. }
  283.  
  284.  
  285. void main(int argc, char *argv[])
  286. {
  287.   if (argc!=3)
  288.   {
  289.     puts("Usage: CONSOLE <device> <unit>");
  290.  
  291.   }
  292.   else
  293.   {
  294.     OpenLibs();
  295.  
  296.     if (Scr=LockPubScreen(NULL))
  297.     {
  298.  
  299.       if (Win=OpenWindowTags(NULL,
  300.                              WA_Width, 640,
  301.                              WA_Height, 256,
  302.                              WA_MaxWidth,65530,
  303.                              WA_MaxHeight,65530,
  304.                              WA_MinWidth,100,
  305.                              WA_MinHeight,100,
  306.                              WA_Title,"ASync Console.device and Serial.device By Hydra/TSN/LSD",
  307.                              WA_Flags,WFLG_CLOSEGADGET|WFLG_DRAGBAR|WFLG_SIZEGADGET|WFLG_SIZEBRIGHT|WFLG_DEPTHGADGET,
  308.                              WA_IDCMP,IDCMP_CLOSEWINDOW,
  309.                              (TAG_DONE)))
  310.       {
  311.  
  312.         if (ConRPort=CreatePort(0,0))
  313.         {
  314.           if (ConWPort=CreatePort(0,0))
  315.           {
  316.             if(ConWrite = (struct IOStdReq *) CreateExtIO(ConWPort,(LONG)sizeof(struct IOStdReq)))
  317.             {
  318.               if(ConRead = (struct IOStdReq *) CreateExtIO(ConRPort,(LONG)sizeof(struct IOStdReq)))
  319.               {
  320.                 if(!(OpenConsole(ConWrite,ConRead,Win)))
  321.                 {
  322.                   if (SerPort=CreatePort(0,0) )
  323.                   {
  324.                     if (SerWrite=(struct IOExtSer *) CreateExtIO(SerPort,(ULONG)sizeof(struct IOExtSer)) )
  325.                     {
  326.                       SerWrite->io_SerFlags       = SERF_RAD_BOOGIE|SERF_XDISABLED|SERF_7WIRE;
  327.                       if (!(OpenDevice(argv[1],atoi(argv[2]),(struct IORequest *)SerWrite,0) ))
  328.                       {
  329.                         SerWrite->IOSer.io_Command  = SDCMD_SETPARAMS;
  330.                         SerWrite->io_SerFlags       |= SERF_XDISABLED;
  331.                         SerWrite->io_Baud           = 19200;
  332.                         DoIO((struct IORequest *)SerWrite);
  333.  
  334.                         if (SerRead=(struct IOExtSer *) AllocMem(sizeof(struct IOExtSer),MEMF_PUBLIC) )
  335.                         {
  336.                           memcpy(SerRead,SerWrite,sizeof(struct IOExtSer));
  337.                           HandleIt();
  338.                           FreeMem(SerRead,sizeof(struct IOExtSer));
  339.                         }
  340.                         CloseDevice((struct IORequest *)SerWrite);
  341.                       } else puts("Can't open device!");
  342.                       DeleteExtIO((struct IORequest*)SerWrite);
  343.                     }
  344.                     DeletePort(SerPort);
  345.                   }
  346.                   CloseDevice((struct IORequest*)ConWrite);
  347.                 } else puts("cant open console device!");
  348.                 DeleteExtIO((struct IORequest*)ConRead);
  349.               }
  350.               DeleteExtIO((struct IORequest*)ConWrite);
  351.             }
  352.             DeletePort(ConWPort);
  353.           }
  354.           DeletePort(ConRPort);
  355.         }
  356.  
  357.         CloseWindow(Win);
  358.       }
  359.  
  360.  
  361.       UnlockPubScreen(NULL,Scr);
  362.     }
  363.     CloseLibs();
  364.   }
  365. }